package com.ebai.product.management.controller.system;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ebai.product.management.base.cache.service.PublicCacheImpl;
import com.ebai.product.management.base.controller.WebController;
import com.ebai.product.management.base.utils.Common;
import com.ebai.product.management.framework.utils.security.ShiroUtils;
import com.ebai.product.management.model.entity.Dept;
import com.ebai.product.management.service.system.DeptService;

import net.sf.json.JSONObject;

/**
 * 系统 部门管理 控制跳转类
 * @author mgli
 *
 */
@Controller
@RequestMapping("/system/dept")
public class DeptController extends WebController{

	@Autowired
	private DeptService deptService;
	
	@RequestMapping("/index")
	@RequiresPermissions("system:dept:view")
    public String indexPage(ModelMap modelMap) {
        modelMap.addAttribute("codeTypeList", PublicCacheImpl.getDictListByCode("TYSF"));
        return "system/dept/index";
    }

    /**
     * AJAX调用分页数据信息
     * @param response HTTP响应
     * @param page 当前页码
     * @param limit 分页条数
     * @param sort 排序方式
     * @param order 排序字段
     * @param condition 查询条件
     */
    @RequestMapping("/pageList")
    public void jsonPageList(HttpSession session, HttpServletResponse response, int page, int limit, String sort, String order, Dept condition) {
        JSONObject json = Common.getJsonDatagrid(this.deptService.queryPage(page, limit, condition));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, json.toString());
    }
    
    /**
	 * 获取树形表格列表数据
	 * @param response
	 */
	@RequestMapping("/list")
    public void getRoleAuthsAll(HttpServletResponse response) {
        JSONObject result = Common.getJsonResult(this.deptService.selectDeptList());
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }
    
    /**
     * 新增或修改时的表单页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/form")
    public String formPage(ModelMap modelMap) {
        return "system/dept/form";
    }
    
    @GetMapping("/deptTree")
    @ResponseBody
    public List<Map<String, Object>> treeData() {
        List<Map<String, Object>> tree = this.deptService.selectDeptTree();
        return tree;
    }

    /**
     * 表单数据保存
     * @param response
     * @param data
     */
    @RequestMapping("/save")
    public void jsonDataSave(HttpServletResponse response, Dept data) {
        JSONObject result = Common.getJsonResult(this.deptService.saveData(data, ShiroUtils.getUser()));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }

    /**
     * 列表数据删除
     * @param response
     * @param ids
     */
    @RequestMapping("/drop")
    public void jsonDataDrop(HttpServletResponse response, String dataPid) {
        JSONObject result = Common.getJsonResult(this.deptService.dropData(dataPid));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }
}
